//
//  XSelectWalletListView.m
//  Bees
//
//  Created by Xiaohan on 2018/12/22.
//  Copyright © 2018 xiaohan. All rights reserved.
//

#import "XSelectWalletListView.h"
#import "LEEAlert.h"

@interface XSelectWalletListView ()<UITableViewDelegate , UITableViewDataSource>

@end

@implementation XSelectWalletListView



+ (void)showList:(NSArray<Wallet *>*)listArr selectWallet:(Wallet*)selectWallet selectAction:(SelectedBlock)selectAction cancelAction:(CancelBlock)cancelAction importAction:(CancelBlock)importAction{
    XSelectWalletListView *view = [[XSelectWalletListView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth([[UIScreen mainScreen] bounds]), 0) style:UITableViewStylePlain];
    view.array = listArr;
    view.selectedBlock = selectAction;
    view.cancelBlock= cancelAction;
    view.importBlock = importAction;
    view.selectWallet = selectWallet;
    [LEEAlert actionsheet].config
    .LeeItemInsets(UIEdgeInsetsMake(0, 0, 0, 0))
    .LeeHeaderInsets(UIEdgeInsetsMake(-10, 20, 20, 20))
    .LeeAddCustomView(^(LEECustomView *custom) {
        custom.view = view;
        custom.isAutoWidth = YES;
    })
    .LeeActionSheetBottomMargin(-AdaptedWidth(6.5)) // 设置底部距离屏幕的边距为0
    .LeeCornerRadius(AdaptedWidth(13)) // 设置圆角曲率为0
    .LeeConfigMaxWidth(^CGFloat(LEEScreenOrientationType type) {
        // 这是最大宽度为屏幕宽度 (横屏和竖屏)
        return CGRectGetWidth([[UIScreen mainScreen] bounds]);
    })
    .LeeShow().LeeCloseComplete(^(){
        !view.cancelBlock?:view.cancelBlock();
    });
   
}

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    self = [super initWithFrame:frame style:style];
    if (self) {
        //初始化数据
        [self initData];
    }
    return self;
}
- (void)setArray:(NSArray<Wallet *> *)array{
    _array = array;
    [self reloadData];
    CGRect selfFrame = self.frame;
    selfFrame.size.height = array.count * XWalletListTableViewCellHeight + AdaptedWidth(44.0f) + AdaptedWidth(37.0f);
    self.frame = selfFrame;
}
#pragma mark - 初始化数据

- (void)initData{
    self.backgroundColor = [UIColor clearColor];
    self.delegate = self;
    self.dataSource = self;
    self.bounces = NO;
    self.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.separatorColor = [[UIColor grayColor] colorWithAlphaComponent:0.2f];
    UIView*headV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, AdaptedWidth(44.0f))];
    [headV addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithActionBlock:^(id  _Nonnull sender) {
        [LEEAlert closeWithCompletionBlock:^{
            
        }];
    }]];
    self.tableHeaderView = headV;
    UIImageView *lineImgV = [UIImageView new];
    lineImgV.image = IMAGE_NAMED(@"wallet_two_line");
    [headV addSubview:lineImgV];
    [lineImgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.centerY.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(AdaptedWidth(27), AdaptedWidth(8)));
    }];
    
    UIView*footV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, AdaptedWidth(37.0f))];
    footV.backgroundColor = UIColorHex(#F7F7F7);
    footV.layer.cornerRadius = footV.height*0.5;
    UILabel *footTitleLab = [UILabel new];
    footTitleLab.text = XLocalized(@"导入钱包");
    footTitleLab.textColor = UIColorHex(#666666);
    footTitleLab.font = kDefaultFont(16);
    [footV addSubview:footTitleLab];
    [footTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo(AdaptedWidth(14));
    }];
    UIImageView*arrow = [UIImageView new];
    arrow.image = IMAGE_NAMED(@"gray_right_arrow");
    [footV addSubview:arrow];
    [arrow mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(AdaptedWidth(-14));
        make.centerY.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(AdaptedWidth(6), AdaptedWidth(12)));
    }];
    self.tableFooterView = footV;
    kWeakSelf(self);
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithActionBlock:^(id  _Nonnull sender) {
        [LEEAlert closeWithCompletionBlock:^{
            !weakself.importBlock?:weakself.importBlock();
        }];
    }];
    [footV addGestureRecognizer:tap];
//    gray_right_arrow@2x
}

#pragma mark - UITableViewDelegate , UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.array.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return XWalletListTableViewCellHeight;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    XWalletListTableViewCell *cell = [XWalletListTableViewCell cellForTableView:tableView];
        
    Wallet *model = self.array[indexPath.row];
    cell.nameLab.text =  model.wallet_name;
    cell.subLab.text  =  model.wallet_public_key;
    if ([model.wallet_uid isEqualToString:self.selectWallet.wallet_uid]) {
        cell.arrowImgV.image = IMAGE_NAMED(@"wallet_select");
    }else{
        cell.arrowImgV.image = IMAGE_NAMED(@"black_right_arrow");        
    }
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    id model = self.array[indexPath.row];
    [LEEAlert closeWithCompletionBlock:^{
       if (self.selectedBlock) self.selectedBlock(model);
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }];
}

@end


///
@implementation XWalletListTableViewCell

- (void)setupUI{
//    self.iconImgV = [UIImageView new];
//    [self.contentView addSubview:self.iconImgV];
//    [self.iconImgV mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(0);
//        make.centerY.mas_equalTo(0);
//        make.size.mas_equalTo(CGSizeMake(AdaptedWidth(35), AdaptedWidth(35)));
//    }];
//    ViewRadius(self.iconImgV, AdaptedWidth(17.5));
    
    self.nameLab = [UILabel new];
    self.nameLab.textColor = UIColor.blackColor;
    self.nameLab.font = kBoldFont(16);
    [self.contentView addSubview:self.nameLab];
    [self.nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.top.mas_equalTo(AdaptedWidth(5));
    }];
    self.arrowImgV = [UIImageView new];
    self.arrowImgV.contentMode = UIViewContentModeScaleAspectFit;
    [self.contentView addSubview:self.arrowImgV];
    [self.arrowImgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(0);
        make.centerY.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(AdaptedWidth(15), AdaptedWidth(15)));
    }];
    
    self.subLab = [UILabel new];
    self.subLab.textColor = UIColorHex(#B1B1B1);
    self.subLab.font = kDefaultFont(12);
    self.subLab.lineBreakMode = NSLineBreakByTruncatingMiddle;
    [self.contentView addSubview:self.subLab];
    [self.subLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.bottom.mas_equalTo(AdaptedWidth(-5));
        make.width.mas_equalTo(AdaptedWidth(150));
    }];
}
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
    [super setHighlighted:highlighted animated:animated];
    if (self.highlighted) {
        POPBasicAnimation *scaleAnimation = [POPBasicAnimation animationWithPropertyNamed:kPOPViewScaleXY];
        scaleAnimation.duration = 0.1;
        scaleAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(0.95, 0.95)];
        [self.nameLab pop_addAnimation:scaleAnimation forKey:@"scaleAnimation"];
    } else {
        POPSpringAnimation *scaleAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPViewScaleXY];
        scaleAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(1, 1)];
        scaleAnimation.velocity = [NSValue valueWithCGPoint:CGPointMake(2, 2)];
        scaleAnimation.springBounciness = 20.f;
        [self.nameLab pop_addAnimation:scaleAnimation forKey:@"scaleAnimation"];
    }
}

@end
